package com.example.hfSystem.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.example.hfSystem.entity.pojo.Token;
import com.example.hfSystem.dao.TokenMapper;
import com.example.hfSystem.service.TokenService;

import com.example.hfSystem.utils.CommonUtil;
import com.example.hfSystem.utils.PageUtils;
import com.example.hfSystem.utils.Query;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * token表 服务实现类
 * </p>
 *
 * @author <a href="https://fengwenyi.com?fs=mpcg">Erwin Feng</a>
 * @since 2024-04-09
 */
@Service
public class TokenServiceImpl extends ServiceImpl<TokenMapper, Token> implements TokenService {

    @Override
    public Token getToken(String token) {
        Token tokenEntity = this.selectOne(new EntityWrapper<Token>().eq("token", token));
        if (tokenEntity == null || tokenEntity.getExpiratedTime().getTime() < new Date().getTime()) {
            return null;
        }
        return tokenEntity;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<Token> page = this.selectPage(
                new Query<Token>(params).getPage(),
                new EntityWrapper<Token>()
        );
        return new PageUtils(page);
    }

    @Override
    public List<Token> selectListView(Wrapper<Token> wrapper) {
        return baseMapper.selectListView(wrapper);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params,
                               Wrapper<Token> wrapper) {
        Page<Token> page = new Query<Token>(params).getPage();
        page.setRecords(baseMapper.selectListView(page, wrapper));
        PageUtils pageUtil = new PageUtils(page);
        return pageUtil;
    }

    @Override
    public String generateToken(Long userid, String username, String tableName, String role) {
        Token tokenEntity = this.selectOne(new EntityWrapper<Token>().eq("user_id", userid).eq("role", role));
        String token = CommonUtil.getRandomString(32);
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.HOUR_OF_DAY, 1);
        if (tokenEntity != null) {
            tokenEntity.setToken(token);
            tokenEntity.setExpiratedTime(cal.getTime());
            this.updateById(tokenEntity);
        } else {
            Token tokens = new Token();
            tokens.setToken(token);
            tokens.setUserName(username);
            tokens.setUserId(userid);
            tokens.setExpiratedTime(cal.getTime());
            tokens.setTablename(tableName);
            this.insert(tokens);
        }
        return token;
    }

    @Override
    public Token getTokenEntity(String token) {
        Token tokenEntity = this.selectOne(new EntityWrapper<Token>().eq("token", token));
        if (tokenEntity == null || tokenEntity.getExpiratedTime().getTime() < new Date().getTime()) {
            return null;
        }
        return tokenEntity;
    }
}
